package com.taxer.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.taxer.contants.Constants;
import com.taxer.db.entity.Admin;
import com.taxer.db.entity.News;
import com.taxer.exception.TaxerExcepiton;
import com.taxer.result.Result;
import com.taxer.service.NewsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping(value = "/news")
public class NewsController {
    @Resource
    private NewsService newsService;

    @RequestMapping("/remove")
    @ResponseBody
    public Result deleteById(@RequestBody News news){
        Result result = new Result();
        try {
            if (news.getId()==null){
                throw new TaxerExcepiton("参数校验失败");
            }else if (news.getId()!=null){
                newsService.deleteById(news.getId());
            }
            result.setMessage("删除成功");
            result.setData("1");

        }catch (TaxerExcepiton taxerExcepiton){
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(@RequestBody News news){
        Result result = new Result();
        try {
            news.setTitle("公告");
            news.setCreated(new Date());
            news.setUpdated(new Date());
            newsService.insert(news);
            result.setMessage("添加成功");
            result.setData(1);

        }catch (TaxerExcepiton taxerExcepiton){
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }
    @RequestMapping("/findAll")
    @ResponseBody
    public Result findAll(){
        Result result = new Result();
        try {
         result.setData(newsService.getNewsAll());
          result.setMessage("查询成功");

        }catch (TaxerExcepiton taxerExcepiton){
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }
    @RequestMapping("/deleteSome")
    @ResponseBody
    public Result deleteSome(@RequestBody JSONArray jsonArray){
        Result result = new Result();
        int size=jsonArray.size();
        try {
            for (int i=0;i<size;i++) {
               JSONObject object = jsonArray.getJSONObject(i);
               String id=object.getString("id");
                newsService.deleteById(Long.valueOf(id));
            }
            result.setData(1);
            result.setMessage("删除成功");

        }catch (TaxerExcepiton taxerExcepiton){
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;
    }


}
